<?php 

require_once '../config.php';

$url      = $_SERVER['HTTP_REFERER'];
$domain   = parse_url($url, PHP_URL_HOST);
            visitors_update(session_id());
            
$visitor  = visitors_get_by_session_id(session_id());

if ($visitor['administratorId'] == 0) {
  visitors_set_administratorId(session_id(), $domain);
}

?>

function LcWidget() {
  
  this.sessionId    = "<?php echo session_id() ?>";
  this.domain       = "<?php echo $domain ?>";
  this.params       = "";
  
  document.lcWidget = this;
  
  this.draw = function() {
    var body      = document.getElementsByTagName("body")[0];
    var container = document.createElement("div");
    var style     = document.createElement("style");
           
        container.setAttribute("style", "display:block;border:2px solid #ccc;width:284px;height:341px;position:fixed;bottom:10px;right:10px;overflow:hidden;background:url('http://lowcost/visitor/images/background.png')");
        container.setAttribute("id", "LcWidgetContainer");
        
        container.innerHTML =
          /**"<?php echo session_id() ?>" + */ 
          "<table cellspacing=0 cellpadding=0 style=\"height:40px;width:90%;margin:5px 0px 0px 10px;\"><tr>" +
          "<td style=\"width:40px;height:40px;border:0px solid #666;background:url('http://lowcost//visitor/images/avatar.png')\"></td>" + 
          "<td style=\"vertical-align:top;padding:3px;font-size:11px;font-family:arial\"><span style=\"color:#000;font-size:14px\"><b>Иноземцева Татьяна</b></span><br/><span style=\"color:#333\">Продавец-консультант Ninja-chat</span></td>" + 
          "<td style=\"text-align:right;vertical-align:top;padding:3px\">" +
          "<a href=# id=LcHideButton style=\"font-family:Tahoma;font-size:9px;color:#333\"><img src=http://lowcost/visitor/images/minimize.gif></a>" + 
          "<a href=# id=LcShowButton style=\"font-family:Tahoma;font-size:9px;color:#333;display:none\"><img src=http://lowcost/visitor/images/maximize.gif></a>" +
          "</td>" +
          "</tr></table>" +
 
        "<iframe src=http://lowcost/visitor/chat.php?sessionId=" + this.sessionId + " frameborder=none style=\"height:341px;margin:0px;\"></iframe>";
        
        body.appendChild(container);
        
        var hideButton = document.getElementById("LcHideButton");
        var showButton = document.getElementById("LcShowButton");
        
        hideButton.onclick = this.hide;
        showButton.onclick = this.show;
  }
  
  this.hide = function() {
      document.lcWidget.params = "windowState=hidden";
      
      document.getElementById("LcShowButton").style.display = "block";
      document.getElementById("LcHideButton").style.display = "none";
      document.getElementById("LcWidgetContainer").style.height = "50px";    
  }
  
  this.show = function(e) {
    try {
      document.lcWidget.params = "windowState=shown";
      document.getElementById("LcHideButton").style.display = "block";
      document.getElementById("LcShowButton").style.display = "none";
      document.getElementById("LcWidgetContainer").style.height = "341px";
    } catch (e) {
      alert("LcWidget.show" + e);
    }    
  }  
  
  this.ping = function () {
    try {
      
      var pingScript = document.getElementById('lowcost1');

      if (pingScript) {
        document.getElementsByTagName('head')[0].removeChild(pingScript);
      }

      var fileref = document.createElement('script');
          
          fileref.setAttribute('type',  'text/javascript');
          fileref.setAttribute('src',   'http://lowcost/visitor/ajax/ping.php?timestamp=' + new Date().getTime() + '&sessionId=' + this.sessionId + "&" + this.params);
          fileref.setAttribute('id',    'lowcost1');

          document.getElementsByTagName("head")[0].appendChild(fileref);
          setTimeout("document.lcWidget.ping()", 3000);
          
          this.params = "";

    } catch(e) {
      alert('LcWidget.ping:' + e);
    }
  }
  
  document.lcWidget = this;
  this.draw();
  this.hide();
  this.ping();
}

new LcWidget();